const authModule = {
    currentUser: null,

    showLogin() {
        document.getElementById('login-form').classList.remove('hidden');
        document.getElementById('register-form').classList.add('hidden');
    },

    showRegister() {
        document.getElementById('login-form').classList.add('hidden');
        document.getElementById('register-form').classList.remove('hidden');
    },

      login() {
        const username = document.getElementById('login-username').value;
        const password = document.getElementById('login-password').value;

        if (!username || !password) {
            alert('请输入用户名和密码！');
            return;
        }

        const user = dataModule.validateUser(username, password);
        if (user) {
            this.currentUser = username;
            document.getElementById('current-user').textContent = username;
            document.getElementById('auth-container').classList.add('hidden');
            document.getElementById('main-container').classList.remove('hidden');
            // 登录成功后加载图书列表
            booksModule.loadBooks();
        } else {
            alert('用户名或密码错误！');
        }
    },


    
    register() {
        const username = document.getElementById('register-username').value;
        const password = document.getElementById('register-password').value;
        const confirmPassword = document.getElementById('register-password-confirm').value;

        if (!username || !password || !confirmPassword) {
            alert('请填写所有字段！');
            return;
        }

        if (password !== confirmPassword) {
            alert('两次输入的密码不一致！');
            return;
        }

        if (dataModule.addUser(username, password)) {
            alert('注册成功！请登录');
            this.showLogin();
        } else {
            alert('用户名已存在！');
        }
    },

        logout() {
        this.currentUser = null;
        document.getElementById('auth-container').classList.remove('hidden');
        document.getElementById('main-container').classList.add('hidden');
        document.getElementById('login-username').value = '';
        document.getElementById('login-password').value = '';
    }
}; 